package com.luck.easyfoodbatch.mvp.home;

import android.content.Context;
import android.support.annotation.Nullable;

import com.luck.easyfoodbatch.callback.JsonCallback;
import com.luck.easyfoodbatch.mvp.model.SearchGoods;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.cache.CacheMode;
import com.lzy.okhttputils.model.HttpParams;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/4/26.
 */

public class ScanGoodSearchPresenter implements ScanGoodSearchModel{
    private final String TAG=getClass().getSimpleName();
    private ScanGoodSearchView mScanGoodSearchView;
    private Context mContext;

    public ScanGoodSearchPresenter(ScanGoodSearchView scanGoodSearchView, Context context) {
        this.mScanGoodSearchView = scanGoodSearchView;
        this.mContext = context;
    }


    @Override
    public void searchGoodsByScan(int page,String bar_code,String province) {
        HttpParams params = new HttpParams();
        params.put("page", page + "");
        params.put("bar_code",bar_code);
        params.put("province", province);
        OkHttpUtils.post(HttpUtil.GOODS_LISTS).tag(this).cacheMode(CacheMode.DEFAULT).cacheKey(TAG)
                .params(params).execute(new JsonCallback(mContext,SearchGoods.class) {
            @Override
            public void onResponse(boolean isFromCache, Object data, Request request, @Nullable Response response) {
                mScanGoodSearchView.getScanGoodSearchSuccess((SearchGoods) data);
                mScanGoodSearchView.hideLoading();
            }

            @Override
            public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                super.onError(isFromCache, call, response, e);
                mScanGoodSearchView.hideLoading();
                mScanGoodSearchView.httpStatusError();
            }
        });
    }
}
